import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.naming.ldap.Control;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.security.Key;
import java.util.List;
import java.util.Set;


import static java.lang.Thread.sleep;


public class Test {
    public static void main(String[] args) throws InterruptedException, IOException {
        //demo1();
        //demo2();
        //demo8();
        //Test01();
        //Test02();
        //Test03();
        //Test04();
        Test05();
    }

    private static void Test05() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("file:///E:/idea---java/selenium/src/main/java/Page/test05.html");
        sleep(3000);
        webDriver.findElement(By.cssSelector("body > input[type=file]")).sendKeys("C:\\Users\\周建荣\\Desktop\\jdk-api-1.8.chw");
        //webElement.click();
    }

    private static void Test04() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("file:///E:/idea---java/selenium/src/main/java/Page/test04.html");
        WebElement webElement = webDriver.findElement(By.cssSelector("body > button"));
        webElement.click();
        webDriver.switchTo().alert().dismiss();
        sleep(3000);
        webElement.click();
        String input = "abcdefgh";
        webDriver.switchTo().alert().sendKeys(input);
        sleep(3000);
        webDriver.switchTo().alert().accept();
    }

    private static void Test03() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("file:///E:/idea---java/selenium/src/main/java/Page/test03.html");
        sleep(3000);
        WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));
        Select select = new Select(webElement);
        sleep(3000);
        select.selectByIndex(2);
        sleep(3000);
        select.selectByValue("7.45");
        webDriver.quit();
    }

    private static void Test02() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/selenium/selenium/src/main/java/Page/test02.html?_ijt=psnvvf6r3hhbq40qaofqr1halh&_ij_reload=RELOAD_ON_SAVE");
        webDriver.switchTo().frame("f1");
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
        sleep(3000);
        webDriver.quit();
    }
    private static void Test01() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/selenium/selenium/src/main/java/Page/test01.html?_ijt=cc056o2fjg5bgdajj2seanc963&_ij_reload=RELOAD_ON_SAVE");
        List<WebElement> list = webDriver.findElements(By.cssSelector("input"));
        for(WebElement x : list){
            if(x.getAttribute("type").equals("checkbox")){
                x.click();
            }
        }
        sleep(3000);
        webDriver.quit();
    }
    private static void demo8() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("黑丝");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //截图
        File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file, new File("D://截图-2024-1-24.png"));
        webDriver.quit();
    }
    private static void demo7() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("美女");

        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);

        //鼠标动作
        Actions actions = new Actions(webDriver);
        WebElement target = webDriver.findElement(By.cssSelector("#\\31  > div > h3 > a"));
        actions.moveToElement(target).contextClick().perform();//移动鼠标并且右击
        sleep(3000);

        webDriver.quit();
    }

    private static void demo6() throws  InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);

        //找到相应的网址
        Set<String> set = webDriver.getWindowHandles();
        String url = "";
        for(String x : set){
            url = x;
        }
        //跳转到该网址
        webDriver.switchTo().window(url);

        webDriver.findElement(By.cssSelector("#ww")).sendKeys("经济");
        sleep(3000);

        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
        sleep(3000);
        webDriver.quit();
    }
    private static void demo5() throws  InterruptedException{
        //打开浏览器
        WebDriver webDriver = new ChromeDriver();
        //打开网页
        webDriver.get("https://www.baidu.com");
        //输入bili
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("bili");
        sleep(2000);
        //点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(2000);
        //ctrl + a
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"A");
        sleep(2000);
        //ctrl + x
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"X");
        sleep(2000);
        //回退
        webDriver.navigate().back();
        //ctrl + v
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"V");
        sleep(2000);
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(2000);
        //下拉框
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        sleep(2000);
        //关闭浏览器
        webDriver.quit();
    }
    private static void demo4() throws  InterruptedException{
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("bilibili");
        sleep(1000);
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(1000);
        webDriver.navigate().back();
        sleep(1000);
        webDriver.navigate().forward();
        System.out.println(webDriver.getCurrentUrl());
        webDriver.quit();
    }
    private static void demo3() throws InterruptedException{
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.manage().window().maximize();
        String s = webDriver.findElement(By.cssSelector("#bottom_layer > div > p:nth-child(1) > a")).getText();
        System.out.println(s);
        webDriver.quit();
    }
    private static void demo2() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        WebElement webElement = webDriver.findElement(By.cssSelector("#kw"));
        webElement.sendKeys("软件测试");
        //webElement.clear();清空输入框中的数据
        WebElement search = webDriver.findElement(By.cssSelector("#su"));
        search.click();
        //search.submit();该方法只能在form标签中使用
        sleep(3000);

        //webDriver.manage().timeouts().implicitlyWait(5, SECONDS);//隐式等待


        List<WebElement> list = webDriver.findElements(By.cssSelector("#\\31  > div._aladdin_zc167_1.aladdin_1w9Kv.is-entry_4Hum4 > h3 > a > div > div > p > span > span > em"));
        for(WebElement webElement1 : list){
            if(webElement1.getText().equals("软件测试")){
                System.out.println("success");
            } else {
                System.out.println("failed");
            }
        }
        webDriver.quit();
    }

    private static void demo1() {
        WebDriver webDriver = new ChromeDriver();
        //webDriver.get("https://www.bilibili.com/");
        //WebElement webElement = webDriver.findElement(By.cssSelector("#i_cecream > div.bili-feed4 > div.bili-header.large-header > div.bili-header__channel > div.right-channel-container > div.channel-items__left > a:nth-child(1)"));
        webDriver.get("https://www.baidu.com/");
//        WebElement webElement = webDriver.findElement(By.cssSelector("#kw"));
//        WebElement webElement = webDriver.findElement(By.xpath("//*[@id=\"i_cecream\"]/div[2]/div[1]/div[1]/ul[1]/li[1]/a/span"));
        WebElement webElement = webDriver.findElement(By.tagName("input"));

        WebDriverWait webDriverWait = new WebDriverWait(webDriver, 3);
        webDriverWait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#s-top-left > a:nth-child(1)")));
        Boolean flg = webDriverWait.until(ExpectedConditions.textToBe(By.cssSelector("#s-top-left > a:nth-child(1)"),"新闻"));
        System.out.println(flg);
        System.out.println(webDriver.getCurrentUrl());
        System.out.println(webDriver.getTitle());
        if(webElement == null) {
            System.out.println("测试不通过");
        }else{
            System.out.println("测试通过");
        }
        webDriver.quit();//能把cookie也删除了
        //webDriver.close();//只是把浏览器关了
    }

}
